package ssm.scheduledJob;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import ssm.dao.Teacher;
import ssm.dao.TeacherCheckWork;
import ssm.dao.TeacherCheckWorkAll;
import ssm.dao.WorkArrange;
import ssm.model.TeacherCheckWorkAllMapper;
import ssm.model.TeacherCheckWorkMapper;
import ssm.model.TeacherMapper;
import ssm.model.WorkArrangeMapper;
import ssm.utils.Const;
import ssm.utils.TimeUtils;

import java.sql.Time;
import java.time.LocalDate;
import java.util.List;
@Component
public class CheckWorkAll {

    @Autowired
    private TeacherCheckWorkMapper teacherCheckWorkMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TeacherCheckWorkAllMapper teacherCheckWorkAllMapper;
    @Autowired
    private WorkArrangeMapper workArrangeMapper;
    /**
     * 用于处理时间批量时间
     * @param startDate 开始时间(含)
     * @param endDate 结束时间  (含)
     */
    public void updateCheckWorkAllByDateList(String startDate,String endDate) {

        List<String> dates=TimeUtils.getListDate(startDate,endDate);
        int i = teacherCheckWorkAllMapper.deleteByList(dates);
        System.out.println("已删除覆盖数据:"+i+"条");
        for(String date:dates){
            this.updateCheckWorkAllByDate(date);
        }
    }

    public void updateCheckWorkAllByToday() {
        //删除表中对应日期数据

       java.lang.String today= TimeUtils.getToday();
        int i= teacherCheckWorkAllMapper.deleteByDate(today);
        System.out.println("已删除覆盖数据:"+i+"条");
       this.updateCheckWorkAllByDate(today);
    }


    public void  updateCheckWorkAllByDate(String date){
        List<TeacherCheckWork> checks = teacherCheckWorkMapper.getCheckWork(date);
        //获取所有在校教师
        List<Teacher> teachers = teacherMapper.getTeacherAll();
        WorkArrange workArrange=workArrangeMapper.selectByDate(date);

        if(workArrange.getStatus()==Const.workArrangeStatus.SHANGBAN.getStatus()||
                workArrange.getStatus()==Const.workArrangeStatus.BUBAN.getStatus()||
                workArrange.getStatus()==Const.workArrangeStatus.QITAZAIXIAO.getStatus()){//上班
            this.isJIARI(teachers, checks,date);
        } else { //假期
            this.noJIARI(teachers, checks,date);
        }
    }






    //非假日考勤信息处理
    public void noJIARI(List<Teacher> teachers, List<TeacherCheckWork> checks,String today) {
        for (Teacher t : teachers) {
            boolean flags = true;
            for (TeacherCheckWork c : checks) {
                TeacherCheckWorkAll jiaban = this.overtime(t, c);
                if (jiaban != null) {
                    System.out.println("jiaban:"+jiaban);
                    //加到库中
                    teacherCheckWorkAllMapper.insert(jiaban);
                    flags = false;//关闸
                    break;
                } else {
                    TeacherCheckWorkAll qingjia = this.leave(t, c);
                    if (qingjia != null) {
                        System.out.println("qingjia:"+qingjia);
                        //加到库中
                        teacherCheckWorkAllMapper.insert(qingjia);
                        flags = false;//关闸
                        break;
                    }

                }
            }
            if (flags) {
                TeacherCheckWorkAll all = this.work(t,today);
                //加到库中
                System.out.println("all:"+all);
                teacherCheckWorkAllMapper.insert(all);
            }
        }


        }
    //假日考勤信息处理
    public void isJIARI(List<Teacher> teachers, List<TeacherCheckWork> checks,String today) {
        for (Teacher t : teachers) {
            boolean flags = true;
            for (TeacherCheckWork c : checks) {
                TeacherCheckWorkAll all = this.overtime(t, c);
                if (all != null) {
                    //加到库中
                    teacherCheckWorkAllMapper.insert(all);
                    flags = false;//关闸
                    break;
                }
            }
            if (flags) {
                TeacherCheckWorkAll all = this.unWork(t,today);
                System.out.println("all:"+all);
                //加到库中
                teacherCheckWorkAllMapper.insert(all);
            }
        }
    }
    //处理加班人员
    public TeacherCheckWorkAll overtime(Teacher teacher, TeacherCheckWork teacherCheckWork) {
        TeacherCheckWorkAll all = new TeacherCheckWorkAll();
        if(teacherCheckWork==null){
            return null;
        }
        if ((teacherCheckWork.getAllstatus() == 1 || teacherCheckWork.getAllstatus() == 3)
                && teacher.getGh().equals(teacherCheckWork.getYhm())) {
            all.setRq(teacherCheckWork.getRq());
            all.setYhm(teacherCheckWork.getYhm());
            all.setYhxm(teacher.getXm());
            all.setMorning(teacherCheckWork.getStatus1());
            all.setAfternoon(teacherCheckWork.getStatus2());
            all.setEvening(teacherCheckWork.getStatus3());
            all.setMidnight(teacherCheckWork.getStatus4());
            String code = "";
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getMorning())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getMorning())) {
                code += 0;
            } else {
                code += 1;
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getAfternoon())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getAfternoon())) {
                code += 0;
            } else {
                code += 1;
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getEvening())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getEvening())) {
                code += 0;
            } else {
                code += 1;
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getMidnight())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getMidnight())) {
                code += 0;
            } else {
                code += 1;
            }
            all.setStatuscode(code);
            return all;
        }else{
            return null;
        }


    }
    //处理请假人员
    public TeacherCheckWorkAll leave(Teacher teacher, TeacherCheckWork teacherCheckWork) {
        if(teacherCheckWork==null){
            return null;
        }
        TeacherCheckWorkAll all = new TeacherCheckWorkAll();
        if ((teacherCheckWork.getAllstatus() == 2)
                && teacher.getGh().equals(teacherCheckWork.getYhm())) {
            all.setRq(teacherCheckWork.getRq());
            all.setYhm(teacherCheckWork.getYhm());
            all.setYhxm(teacher.getXm());
            all.setMorning(teacherCheckWork.getStatus1());
            all.setAfternoon(teacherCheckWork.getStatus2());
            all.setEvening(teacherCheckWork.getStatus3());
            all.setMidnight(teacherCheckWork.getStatus4());
            StringBuffer code = new StringBuffer();
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getMorning())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getMorning())) {
                code.append(0);
            } else {
                code.append(1);
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getAfternoon())||
                    Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getAfternoon())) {
                code.append(0);
            } else {
                code.append(1);
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getEvening())||
            Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getEvening())) {
                code.append(0);
            } else {
                code.append(1);
            }
            if (Const.StatusArray.YINGONGWAICHU.getList().contains(teacherCheckWork.getMidnight())||
            Const.StatusArray.JIAQI.getList().contains(teacherCheckWork.getMidnight())) {
                code.append(0);
            } else {
                code.append(1);
            }
            all.setStatuscode(code.toString());
            return all;
        }else{
            return null;
        }

    }
    //处理正常上班人员
    public TeacherCheckWorkAll work(Teacher teacher, String today) {
        TeacherCheckWorkAll all = new TeacherCheckWorkAll();
        all.setRq(today);
        all.setYhm(teacher.getGh());
        all.setYhxm(teacher.getXm());
        all.setMorning("正常上班");
        all.setAfternoon("正常上班");
        all.setEvening("下班休息");
        all.setMidnight("下班休息");
        all.setStatuscode("1100");
        return all;
    }
    //处理正常不上班人员
    public TeacherCheckWorkAll unWork(Teacher teacher, String today) {
        TeacherCheckWorkAll all = new TeacherCheckWorkAll();
        all.setRq(today);
        all.setYhm(teacher.getGh());
        all.setYhxm(teacher.getXm());
        all.setMorning("周末休息");
        all.setAfternoon("周末休息");
        all.setEvening("周末休息");
        all.setMidnight("周末休息");
        all.setStatuscode("0000");
        return all;
    }



}
